package org.longgang.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.longgang.bean.Admin;
import org.longgang.service.AdminService;
import org.longgang.tools.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class AdminController {

	@Autowired
	private AdminService service;

//	添加管理员
	@RequestMapping("/insertAdmin")
	public Object insertAdmin(HttpServletRequest request) {
		String name = request.getParameter("name");
		String password = request.getParameter("password");
		String b1 = request.getParameter("b1");
		Integer i = service.insertAdmin(new Admin(0, name, password, b1, ""));

		ResponseData responseData = new ResponseData(5, i, "查询成功！");
		return responseData;
	}

//	登录
	@RequestMapping("/selectAdminId")
	public Object selectAdminId(HttpServletRequest request) {
		String name = request.getParameter("name");
		String password = request.getParameter("password");

		Integer i = service.selectAdminId(new Admin(0, name, password, "", ""));

		HttpSession session = request.getSession();
		session.setAttribute("aid", i);
		System.out.println(request.getSession().getId() + "  aid=" + request.getSession().getAttribute("aid"));

		ResponseData responseData = new ResponseData(5, i, "查询成功！");
		return responseData;
	}

//		new Admin(adminid, name, password, b1, b2)
//	根据权限查询管理员账号
	@RequestMapping("/selectAllAdmin")
	public Object selectAllAdmin(HttpServletRequest request) {
		String b1 = request.getParameter("b1");

		List<Admin> list = service.selectAllAdmin(new Admin(0, "", "", b1, ""));

		ResponseData responseData = new ResponseData(5, list, "查询成功！");
		return responseData;
	}

//	管理员id 更新权限等级
	@RequestMapping("/updateAdmin")
	public Object updateAdmin(HttpServletRequest request) {
		Integer adminid = Integer.parseInt(request.getParameter("adminid"));

		Integer i = service.updateAdmin(new Admin(adminid, "", "", "", ""));

		ResponseData responseData = new ResponseData(5, i, "查询成功！");
		return responseData;
	}

//	根据管理员id 查询信息
	@RequestMapping("/selectAdminById")
	public Object selectAdminById(HttpServletRequest request) {
		Integer adminid = Integer.parseInt(request.getParameter("adminid"));

		Admin admin = service.selectAdminById(new Admin(adminid, "", "", "", ""));

		ResponseData responseData = new ResponseData(5, admin, "查询成功！");
		return responseData;
	}

//	获取session的id
	@RequestMapping("/getAdminSession")
	public Object getCompanySession(HttpServletRequest request) {

		ResponseData responseData = new ResponseData(5, request.getSession().getAttribute("aid"), "");
		return responseData;
	}
}
